<?php

require "conf.php";
require_once "data/dao/BaseDAO.php";
require_once "application/game/Game.php";
require_once "utils/Validate.php";

class GameDAO extends BaseDAO{
	
	/**
	 * add game to game table
	 * 
	 * @param $id
	 * @param $title
	 * @param $manufacturer
	 * @param $releaseDate
	 * @param $imageUrl
	 * @param $amazonUrl
	 * @param $locale
	 * @param $description
	 * 
	 */
	public function addGame($id, $title, $manufacturer, $releaseDate, $imageUrl, $amazonUrl, $locale, $description, $listed){
		
		//TODO: validate uniqueness of game by id
		Validate::notNull($id);
		Validate::notNull($title);
		Validate::notNull($locale);
		Validate::notNull($listed);
		
		$values = array($id, $title, $manufacturer, $releaseDate,
			$imageUrl, $largeImageUrl, $amazonUrl, $locale, $description, $listed);
		
		$this->update("INSERT INTO " . GAME_TABLE . " VALUES('%s', '%s', '%s', '%s', 
			'%s', '%s', '%s', '%s', '%s', '%s')", $values);
	}
	
	public function deleteGame($id){
		
		mysql_query("DELETE FROM " . GAME_TABLE . " WHERE id = '$id'");
	}
	
	/**
	 * searches the table to determin whether or not a game exists
	 * 
	 * @param $id
	 * @return boolean
	 */
	public function gameExists($id){
		
		self::connect();
		$find = mysql_query("SELECT * FROM " . GAME_TABLE . " WHERE id = '$id'" );
		
		if(mysql_fetch_array($find))
			return true;
			
		return false;
	}
	
	public function getGamesAlphabetically($index, $locale = null){
		
		$sql = "SELECT * FROM " . GAME_TABLE . " WHERE listed = 1"; 
		if($locale != null)
			$sql .= " AND locale = '$locale'";
		
		$sql .= " ORDER BY title";
		
		self::connect();
		$find = mysql_query($sql);
		
		$sortedGames = array();
		
		while($row = mysql_fetch_assoc($find)){
			$title = $row['title'];
			if($title[0] == $index)
				array_push($sortedGames, new Game($row['id'], $row['title'], $row['manufacturer'],
					$row['release_date'], $row['image_url'], $row['largeImageUrl'], $row['amazon_associate_url'], 
					$row['locale'], $row['description'], $row['listed']));
		}
		return $sortedGames;
	}
	
	public function getGamesByKeyword($keyword, $locale = null){
		
		$sql = "SELECT * FROM " . GAME_TABLE . " WHERE title LIKE '%$keyword%' AND listed = 1";
		if($locale != null)
			$sql .= " AND locale = '$locale'";
			
		$sql .= " ORDER BY title";
			
		self::connect();
		$find = mysql_query($sql);

		$games = array();
		while($row = mysql_fetch_assoc($find)){
			array_push($games, new Game($row['id'], $row['title'], $row['manufacturer'],
				$row['release_date'], $row['image_url'], $row['largeImageUrl'], $row['amazon_associate_url'], 
				$row['locale'], $row['description'], $row['listed']));
		}
		return $games;
	}
	
	//TODO: change this method to use the new queryForObject method
	/**
	 * returns a game that has the associated id, returns null
	 * if nothing found.
	 * 
	 * @param $id
	 * @return Game
	 */
	public function getGame($id){
		
		self::connect();
		
		// removed listed check because a game will only be linked to if it is listed anyway
		$find = mysql_query("SELECT * FROM " . GAME_TABLE . " WHERE id = '$id'");
		
		if(mysql_num_rows($find) == 0)
			return null;
		
		while ($row = mysql_fetch_assoc($find)) {
			return new Game($row['id'], $row['title'], $row['manufacturer'], 
				$row['release_date'], $row['image_url'], $row['largeImageUrl'], $row['amazon_associate_url'], 
				$row['locale'], $row['description'], $row['listed']);
		}
	}

	//TODO: if php ever introduce method overloading (easily), then
	// change this method to getGame($title)
	/**
	 * returns a game that has the associated id, returns null
	 * if nothing found.
	 * 
	 * @param $id
	 * @return Game
	 */
	public function getGameByTitle($title){
		
		$game = $this->queryForObject("select id, title, manufacturer,
			release_date, image_url, largeImageUrl, amazon_associate_url, locale,
			description, listed from games where title = '$title' and listed = 1", "Game");
			
		return $game;
	}
	
	
	public function updateImageUrl($gameId, $imageUrl){
		$this->update("UPDATE " . GAME_TABLE . " SET image_url = '$imageUrl'" .
				"WHERE id = '$gameId'");
	}
	
	public function updateLargeImageUrl($gameId, $imageUrl){
		$this->update("update " . GAME_TABLE . " set largeImageUrl = '$imageUrl'" .
			"where id = '$gameId'");
	}

	public function updateAmazonUrl($gameId, $amazonUrl){
		
		self::connect();
		mysql_query("UPDATE " . GAME_TABLE . " SET amazon_associate_url = '$amazonUrl'" .
				"WHERE id = '$gameId'");
	}

	public function updateDescription($gameId, $description){
		
		$this->update("UPDATE " . GAME_TABLE . " SET description = '%s' WHERE
			id = '%s'", array($description, $gameId));
	}
	
	public function getTotalGames(){
		
		self::connect();
		$find = mysql_query("SELECT * FROM " . GAME_TABLE . " WHERE listed = 1");
		return mysql_num_rows($find);
	}
	
	public function getAllGames($locale = null){
		
		$sql = "SELECT * FROM " . GAME_TABLE . " WHERE listed = 1";
		if ($locale != null)
			$sql .= " AND locale = '$locale'";

		self::connect();
		$find = mysql_query($sql);

		$games = array();
		while($row = mysql_fetch_assoc($find)){
			array_push($games, new Game($row['id'], $row['title'], $row['manufacturer'],
				$row['release_date'], $row['image_url'], $row['largeImageUrl'], $row['amazon_associate_url'], 
				$row['locale'], $row['description'], $row['listed']));
		}
		return $games;
	}
	
	public function delistGame($gameId){
		
		Validate::notNull($gameId);
		
		$this->update("update games set listed = 0 where id = '%s'", array($gameId));
	}
}
?>
